import { createStore, combineReducers, applyMiddleware } from "redux";
import thunk from "redux-thunk";

const initState = { a: 100, userinfo: JSON.parse(localStorage.getItem("userinfo")) || {} };
const homeReducer = (state = { topics: [] }, action) => {
    switch (action.type) {
        case "get_topics_success":
            return {
                ...state,
                topics: action.payload,
            };
        default:
            return state;
    }
};
const loginReducer = (state = initState, action) => {
    switch (action.type) {
        case "loginSuccess":
            return { ...state, userinfo: action.payload };
        case "getUserInfoSuccess":
            return { ...state, usernfo: action.payload }; //如果传递的参数是一个对象时的写法
        // case "logoutSuccess":
        //     return false;
        default:
            return state;
    }
};

const rootReducer = combineReducers({
    a: loginReducer,
    b: homeReducer,
});
// createStore可以将参数传递
const store = createStore(rootReducer, applyMiddleware(thunk));
export default store;

/*
combineReducers提供一个创建多个reducer的方法，传递一个对象，对象里面包含多个reducer
applyMiddleWare提供一个中间件方法，传递thunk参数，thunk是从redux-thunk库中引入的
 */
